import httpUtils from "./XmlHttpTool";
var addItem = cc.Class({
    name: 'addItem',
    properties: {
        titileLabel:cc.String,
        addContent:cc.String,
        addSringNarray:[]
      
    }
});
cc.Class({

    extends: cc.Component,
  
    properties: {
        items: {
            default: [],
            type: addItem
        },
        itemPrefab: cc.Prefab,
        changeAddressBtn:cc.Node,
        isDefualt:cc.Boolean,
        addressAllview:cc.Node,
        addressGameAllview:cc.Node,
        tid:"",
        addinfoDic:null,
        exputNarray:[],
        successPopView:cc.Node,
        successGamePopView:cc.Node,
        isGamePlay:cc.Boolean,
        closeBtn:cc.Button
    },


    onLoad () {

    },

    closeView:function(){


     var changebig2 = cc.scaleTo(0.2,1.1);

     var finish = cc.callFunc(function(){

     var changesmail2 = cc.scaleTo(0.1,1.0);
      this.closeBtn.node.runAction(changesmail2)

     }, this);

    var seq = cc.sequence(changebig2,finish)

    this.closeBtn.node.runAction(seq)

     console.log("关闭地址栏")

        if(this.isGamePlay){
            this.addressGameAllview.active=false
        }else{
            this.addressAllview.active=false
        }
   

    },
    submitClick: function (event) {
       
         

        for (var i = 0; i < this.exputNarray.length-1; ++i) {
     
                var content = this.exputNarray[i].getComponent('addPrefab').node.getChildByName("inputAddressMessage").getComponent(cc.EditBox).string;
                this.addSringNarray.push(content)
     
        }

        if(this.addSringNarray[0]==""){
            console.log("联系人为空")
            return
        }else if(this.addSringNarray[1]==""){
            console.log("联系电话为空")
            return
        }else if(this.addSringNarray[2]==""){
            console.log("地区为空")
            return
        }else if(this.addSringNarray[1]==""){
            console.log("输入地址为空")
            return
        }else{

        }

        var prizedic={'receiver':this.addSringNarray[0],"mobile":this.addSringNarray[1],"district":this.addSringNarray[2],"location":this.addSringNarray[3],"isdefault":this.isDefualt}

        var that=this
        httpUtils.NhttpPost("/marmot/save_packages",{"address_info":prizedic,"tid":this.tid},function(res){

            console.log("兑换成功",res)

            

            if(that.isGamePlay){
                console.log("游戏内兑换成功",that.successGamePopView.getComponent("CraneAlertView"))
                that.addressGameAllview.active=false
                that.successGamePopView.active=true
                that.successGamePopView.getComponent("CraneAlertView").init(res["kf_wx"])

            }else{

                console.log("游戏外兑换成功",that.successPopView.getComponent("CraneAlertView"))
                that.addressAllview.active=false
                that.successPopView.active=true
                that.successPopView.getComponent("CraneAlertView").init(res["kf_wx"])
            }
        
        },function(faileres){

            console.log("兑换失败",res)
        
        })

     },

     changeClick:function(){

        this.isDefualt=!this.isDefualt

        if(this.isDefualt){
            var _this=this
            this.changeAddressBtn.getChildByName("topchangeImage").x=21
            console.log("改变地址",this.changeAddressBtn.getComponent(cc.Sprite).spriteFrame)
             cc.loader.loadRes("开", cc.SpriteFrame, function (err, spriteFrame) {
      
                _this.changeAddressBtn.getComponent(cc.Sprite).spriteFrame = spriteFrame;
           
            });
        }else{
            var _this=this
            this.changeAddressBtn.getChildByName("topchangeImage").x=-19
            console.log("改变地址",this.changeAddressBtn.getComponent(cc.Sprite).spriteFrame)
             cc.loader.loadRes("关", cc.SpriteFrame, function (err, spriteFrame) {
      
                _this.changeAddressBtn.getComponent(cc.Sprite).spriteFrame = spriteFrame;
         
            });
        }
     
    },

    start () {

  
    },

    getAddressInfo:function(){


        if(this.exputNarray.length>0){
            this.node.removeAllChildren();
        }
    
        
        this.isDefualt=true
        this.addSringNarray=[]
        this.exputNarray=[]
    
        for (var i = 0; i < this.items.length; ++i) {
            if(i<5){

                var item = cc.instantiate(this.itemPrefab);
                this.node.addChild(item);
                this.exputNarray.push(item)
                item.getComponent('addPrefab').init(this.items[i]["titileLabel"]);
                if(this.addinfoDic["default_address"]){

                    console.log("存在默认地址")
                    if(i==0){
                  item.getComponent('addPrefab').node.getChildByName("inputAddressMessage").getComponent(cc.EditBox).string=this.addinfoDic["default_address"]["receiver"]
                    }else if(i==1){
                        item.getComponent('addPrefab').node.getChildByName("inputAddressMessage").getComponent(cc.EditBox).string=this.addinfoDic["default_address"]["mobile"]
                    }else if(i==2){
                        item.getComponent('addPrefab').node.getChildByName("inputAddressMessage").getComponent(cc.EditBox).string=this.addinfoDic["default_address"]["district"]
                    }else{
                        item.getComponent('addPrefab').node.getChildByName("inputAddressMessage").getComponent(cc.EditBox).string=this.addinfoDic["default_address"]["location"]
                    }
                    

                }else{

                    console.log("没有默认地址")
                }
              

                if(i==4){

                    item.getComponent('addPrefab').node.getChildByName("inputAddressMessage").active=false
                    item.getComponent('addPrefab').node.getChildByName("sliderBtn").active=true
                    this.changeAddressBtn=item.getComponent('addPrefab').node.getChildByName("sliderBtn")
                    this.changeAddressBtn.on('click', this.changeClick, this);

                              
                }


            }else{
                console.log("最后节点")
                var node = new cc.Node('Sprite');
                node.addComponent(cc.Sprite);     
                node.parent = this.node;
                node.width=400
                node.height=176
                node.x=-76
              

                var imageView = new cc.Node('Sprite');
                imageView.addComponent(cc.Sprite);
                imageView.parent =node;

                cc.loader.load({url: this.addinfoDic["prize_info"]["thumbnail"], type: 'png'}, function(err,img){

                    var mylogo  = new cc.SpriteFrame(img); 
                    imageView.getComponent(cc.Sprite).spriteFrame = mylogo;
                    imageView.width=126
                    imageView.height=126
                    imageView.x=-137
                    
                });

                let that = this

                var imageViewCover = new cc.Node('Sprite');
                imageViewCover.addComponent(cc.Sprite);
                imageViewCover.parent =node;

                cc.loader.loadRes("背包物品框", cc.SpriteFrame, function (err, spriteFrame) {
             
                    imageViewCover.getComponent(cc.Sprite).spriteFrame= spriteFrame;
                    imageViewCover.width=126
                    imageViewCover.height=126
                    imageViewCover.x=-137
     
                });

                var namelabel = new cc.Node('Sprite')
                namelabel.addComponent(cc.Label);
                namelabel.addComponent(cc.LabelOutline)
                namelabel.parent = node;
                var colorCode =  new cc.Color(93,41,41,255);
                namelabel.color=colorCode
               
                namelabel.x=-40
                namelabel.getComponent(cc.Label).fontSize=28
                namelabel.getComponent(cc.Label).horizontalAlign=0
                namelabel.getComponent(cc.LabelOutline).color=colorCode
                namelabel.setAnchorPoint(0, 0.5);
                namelabel.getComponent(cc.LabelOutline).width=0.5
                namelabel.getComponent(cc.Label).string=this.addinfoDic["prize_info"]["name"]

            }   

         
        }
        if(this.addinfoDic["default_address"]["isdefault"]==1){
            this.isDefualt=true
        }else{
            this.isDefualt=false
        }
        
        console.log("是否已存为默认",this.isDefualt)
    },

    init(toyid,addinfo,isGame){

        this.tid=toyid
        console.log("传过来的地址",addinfo)
        this.addinfoDic=addinfo
        this.isGamePlay=isGame

        this.getAddressInfo()
    }

    // update (dt) {},
});
